package com.whut.configuration;

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class JacksonModuleConfig {

    public static final String DEFAULT_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final DateTimeFormatter DEFAULT_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_PATTERN);

    @Bean
    public JavaTimeModule customJavaTimeModule() {
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        //序列化日期格式
        // LocalDateTime 2011-12-03T10:15:30 -> 2019-01-01 00:00:00
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DEFAULT_DATE_TIME_FORMATTER));
        // OffsetDateTime 2011-12-03T10:15:30+01:00-> 2019-01-01 00:00:00
        //javaTimeModule.addSerializer(OffsetDateTime.class, CustomOffsetDateTimeSerializer.INSTANCE);
        // javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(Constants.DEFAULT_DATE_FORMATTER));
        // javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(Constants.DEFAULT_TIME_FORMATTER));
        //反序列化日期格式
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DEFAULT_DATE_TIME_FORMATTER));
        // OffsetDateTime 2011-12-03T10:15:30+01:00-> 2019-01-01 00:00:00
        // javaTimeModule.addDeserializer(OffsetDateTime.class, CustomOffsetDateTimeDeserializer.INSTANCE);
        // javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(Constants.DEFAULT_DATE_FORMATTER));
        // javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(Constants.DEFAULT_TIME_FORMATTER));
        return javaTimeModule;
    }
}
